package com.zjw.controller;


import com.zjw.factory.ProductStrategyFactory;
import com.zjw.factory.SumStrategyFactory;
import com.zjw.strategy.ProductStrategy;
import com.zjw.strategy.SumStrategy;
import com.zjw.vo.OrderDetail;
import com.zjw.vo.ProductDetail;
import com.zjw.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cal")
public class CalSumController {

    @Resource
    private ProductStrategyFactory productStrategyFactory;
    @Resource
    private SumStrategyFactory sumStrategyFactory;

    @PostMapping("/sum")
    public ResultVo calSumPrice(@RequestBody OrderDetail orderDetail){
        String discountOrNot4Sum = orderDetail.getDiscountOrNot();
        SumStrategy strategy4Sum = sumStrategyFactory.getStrategy(discountOrNot4Sum);
        List<ProductDetail> list = orderDetail.getProductList();
        List<Double> priceList = new ArrayList<>();
        for (ProductDetail productDetail : list) {
            ProductStrategy productStrategy = productStrategyFactory.getProductStrategy(productDetail.getDiscountOrNot());
            priceList.add(productStrategy.calPrice(productDetail.getNum(), productDetail.getPrice()));
        }
        double resultSum = strategy4Sum.calSum(priceList);
        return ResultVo.ok(resultSum);
    }
}
